package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/26 17:37
 */
public class PlanManageQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      PlanManageQueryActivity.class.getSimpleName() + "EXTRA_CONTENT";
  private final int BEGINREG_DATE = 0;
  private final int ENDREG_DATE = 1;
  private final int EXC_JOBNO = 2;
  private final int COR_JOBNO = 3;
  @BindView(R.id.tv_beginplanStartTime) TextView tvBeginplanStartTime;
  @BindView(R.id.ib_beginplanStartTime) ImageView ibBeginplanStartTime;
  @BindView(R.id.tv_endplanStartTime) TextView tvEndplanStartTime;
  @BindView(R.id.ib_endplanStartTime) ImageView ibEndplanStartTime;
  @BindView(R.id.tv_excjobno) TextView tvExcjobno;
  @BindView(R.id.ib_excjobno) ImageView ibExcjobno;
  @BindView(R.id.tv_corjobno) TextView tvCorjobno;
  @BindView(R.id.ib_corjobno) ImageView ibCorjobno;

  private String beginplanStartTime;
  private String endplanStartTime;
  private String excjobnoStr;
  private String excnameStr;
  private String corjobnoStr;
  private String cornameStr;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    ExtraValueContent mContent = (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    beginplanStartTime = mContent.getExtName1();
    endplanStartTime = mContent.getExtName2();
    excnameStr = mContent.getExtName3();
    excjobnoStr = mContent.getExtCode3();
    cornameStr = mContent.getExtName4();
    corjobnoStr = mContent.getExtCode4();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(beginplanStartTime);
        queryConten.setExtName2(endplanStartTime);
        queryConten.setExtName3(excnameStr);
        queryConten.setExtCode3(excjobnoStr);
        queryConten.setExtName4(cornameStr);
        queryConten.setExtCode4(corjobnoStr);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_plan_manage_query, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  public void bindData() {
    tvBeginplanStartTime.setText(beginplanStartTime);
    tvEndplanStartTime.setText(endplanStartTime);
    tvExcjobno.setText(excnameStr);
    tvCorjobno.setText(cornameStr);
  }

  @OnClick({
      R.id.ib_beginplanStartTime, R.id.ib_endplanStartTime, R.id.ib_excjobno, R.id.ib_corjobno
  }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_beginplanStartTime:
        showDialog(BEGINREG_DATE);
        break;
      case R.id.ib_endplanStartTime:
        showDialog(ENDREG_DATE);
        break;
      case R.id.ib_excjobno:
        //负责人多选
        Intent excPerson = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        excPerson.putExtra(SearchPersonMultipleFragment.EXTRA_FROM,
            SearchPersonMultipleFragment.VALUE_COMMON);
        excPerson.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, excjobnoStr);
        startActivityForResult(excPerson, EXC_JOBNO);
        break;
      case R.id.ib_corjobno:
        //参与人单选
        Intent cerPerson = new Intent(getActivity(), SearchPersonActivity.class);
        cerPerson.putExtra(SearchPersonListFragment.EXTRA_FROM,
            SearchPersonListFragment.VALUE_COMMON);
        startActivityForResult(cerPerson, COR_JOBNO);
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {

      case BEGINREG_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(beginplanStartTime));
        break;
      case ENDREG_DATE:
        dialog = BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(endplanStartTime));
        break;
      default:
        break;
    }
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginplanStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginplanStartTime.setText(beginplanStartTime);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endplanStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndplanStartTime.setText(endplanStartTime);
    }
    if (requestCode == EXC_JOBNO) {
      //负责人
      excnameStr = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      excjobnoStr = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      tvExcjobno.setText(excnameStr);
    }
    if (requestCode == COR_JOBNO) {
      //参与人
      CustomerDialog person =
          (CustomerDialog) data.getSerializableExtra(SearchPersonListFragment.EXTRA_VALUE);
      cornameStr = person.getName();
      corjobnoStr = person.getJobNo();
      tvCorjobno.setText(cornameStr);
    }
  }

  public static PlanManageQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    PlanManageQueryFragment fragment = new PlanManageQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
